package com.jxdinfo.hussar.example.common.schedulingtask.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxdinfo.hussar.example.common.schedulingtask.dao.SysStruMesMapper;
import com.jxdinfo.hussar.example.common.schedulingtask.dao.SysUsersMesMapper;
import com.jxdinfo.hussar.example.common.schedulingtask.model.SysOrganMes;
import com.jxdinfo.hussar.example.common.schedulingtask.model.SysStruMes;
import com.jxdinfo.hussar.example.common.schedulingtask.scheduleutil.ScheduleUtils;
import com.jxdinfo.hussar.example.common.schedulingtask.service.SysStruService;
import com.jxdinfo.hussar.example.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 客户表 服务实现类
 * </p>
 *
 * @author 乔镔
 * @since 2024-04-03
 */
@Service("com.jxdinfo.hussar.example.common.schedulingtask.service.impl.SysStruServiceImpl")
public class SysStruServiceImpl extends ServiceImpl<SysStruMesMapper, SysStruMes> implements SysStruService {

    @Autowired
    private SysStruMesMapper sysStruMesMapper;

    @Autowired
    private SysUsersMesMapper sysUsersMesMapper;

    @Override
    public void manualSynchronization() {
        //从erp数据库中取出所有部门信息
        List<SysStruMes> sysStrusMes = sysStruMesMapper.selectAllSysStru();
        for (SysStruMes strus : sysStrusMes) {
            SysOrganMes sysOrganMes = new SysOrganMes();  //sys_organ
            //sys_stru
            strus.setORGAN_ALIAS(strus.getORGAN_ALIAS().replace("/","or"));
            strus.setORGAN_ID(strus.getSTRU_ID());    //主键ID
            strus.setSTRU_TYPE("2");                //部门类型是2
            //变换状态
            if (strus.getDEL_FLAG().equals("1")) {
                strus.setDEL_FLAG("0");             //删除标志
            }
            if (strus.getPARENT_ID() == null) {
                strus.setPARENT_ID(1L);
                sysOrganMes.setPARENT_TYPE_CODE("1");
            }else {
                sysOrganMes.setPARENT_TYPE_CODE("2");
            }
            strus.setIS_LEAF("Y");

            for (SysStruMes sysStrusMe : sysStrusMes) {
                if (strus.getSTRU_ID().equals(sysStrusMe.getPARENT_ID())){
                    strus.setIS_LEAF("N");
                }
            }

            List<Long> allParentIds = ScheduleUtils.getAllParentIdsSysStru(sysStrusMes, strus.getSTRU_ID());
            List<String> allParentIdsSysOrganFcode = ScheduleUtils.getAllParentIdsSysOrganFcode(sysStrusMes, strus.getSTRU_ID());
            List<String> allParentIdsSysOrganFname = ScheduleUtils.getAllParentIdsSysOrganFname(sysStrusMes, strus.getSTRU_ID());
            StringBuilder stringBuilder = new StringBuilder();
            StringBuilder stringBuilderOrganFcode = new StringBuilder();
            StringBuilder stringBuilderOrganFname = new StringBuilder();
            for (int i = 0; i < allParentIds.size(); i++) {
                stringBuilder.append("/");
                stringBuilderOrganFcode.append("/1/");
                stringBuilderOrganFname.append("/山东鲁银新材料科技有限公司/");

                Long aLong = allParentIds.get(allParentIds.size()-1-i);
                String OrganFcode = allParentIdsSysOrganFcode.get(allParentIds.size() - 1 - i);
                String OrganFname = allParentIdsSysOrganFname.get(allParentIds.size() - 1 - i);

                stringBuilder.append(aLong);
                stringBuilderOrganFcode.append(OrganFcode);
                stringBuilderOrganFname.append(OrganFname);

                strus.setSTRU_LEVEL(i+2);
            }
            strus.setSTRU_FID(stringBuilder.toString() + "/" + strus.getSTRU_ID() + "/");
            sysOrganMes.setORGAN_FCODE(stringBuilderOrganFcode.toString() + "/");
            sysOrganMes.setORGAN_FNAME(stringBuilderOrganFname.toString());

            String a = "/0001";
            StringBuilder b = new StringBuilder();
            for (int i = 0; i < strus.getSTRU_FID().split("/").length - 1; i++) {
                b.append(a);
            }
            strus.setSTRU_SEQ(b.toString());
            //sys_organ
            sysOrganMes.setORGAN_ID(strus.getORGAN_ID());           //主键
            sysOrganMes.setORGAN_CODE(strus.getORGAN_CODE());           //编码
            sysOrganMes.setORGAN_NAME(strus.getORGAN_ALIAS());           //名称
            sysOrganMes.setSHORT_NAME(strus.getORGAN_ALIAS());           //简称
            sysOrganMes.setORGAN_TYPE(strus.getSTRU_TYPE());           //部门类型
            sysOrganMes.setDEL_FLAG(strus.getDEL_FLAG());           //部门类型

            //根据erp的每个对象去查找mes数据库是否存在这一条数据
            //sys_stru
            SysStruMes sysStruMes = sysStruMesMapper.selectSysStru(strus);
            if (StringUtils.isNull(sysStruMes)){
                sysStruMesMapper.insertSysStru(strus);
                sysUsersMesMapper.insertSysOrgan(sysOrganMes);
            }
        }
    }
}
